package com.sun.officeoa.controller;

import com.sun.officeoa.bean.Department;
import com.sun.officeoa.service.DepartmentService;
import org.dom4j.Entity;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(value = "/office-oa")
public class DepartmentController{


    @Resource
    private DepartmentService departmentService;

    /**
     * @Author SUN-JM
     * @Description //TODO 所有部门信息
     * @Date 18:48 2019/2/28
     * @Param []
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/departmentsAll")
    public ModelAndView departmentsTest(){
        List<Department> list = departmentService.getAllDepartment();
        ModelAndView mv = new ModelAndView();
        mv.addObject("list",list);
        mv.setViewName("/departments");
        return mv;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 删除部门
     * @Date 18:48 2019/2/28
     * @Param [id]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/deletedep")
    public ModelAndView deleteDepartment(@RequestParam("depid")int id){
        ModelAndView mv = new ModelAndView();
        int deptId = departmentService.deleteDepById(id);
        if(deptId == 0){
            mv.addObject("error","删除失败");
            mv.setViewName("/departments");
        }else{
            mv.setViewName("redirect:/office-oa/departmentsAll");
        }
        return mv;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 新增部门
     * @Date 18:49 2019/2/28
     * @Param [name]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/adddep")
    public ModelAndView  adddep(@RequestParam("departmentname") String name){
        ModelAndView mv = new ModelAndView();
        if(!ObjectUtils.isEmpty(name)){
            int deptId = departmentService.insert(name);
            if(deptId == 0){
                mv.addObject("error","删除失败");
                mv.setViewName("/departments");
            }else{
                mv.setViewName("redirect:/office-oa/departmentsAll");
            }
        }else{
            mv.addObject("error","删除失败");
            mv.setViewName("/departments");
        }
        return mv;

    }



}
